/*
 * For legal stuff see the file COPYRIGHT
 */
#import "SessionEditor.h"
#import "Controller.h"

@implementation SessionEditor

- awakeFromNib
{
  [descriptionField setScrollable:YES];
  return self;
}

/*
 * Allow only one SessionEditor object to be created.
 */
+ new
{
  static id inspector;

  if ( ! inspector ) {
    inspector = [[SessionEditor alloc] init];
    [NXApp loadNibSection:"SessionEditor.nib" owner:inspector withNames:NO];
  }

  return inspector;
}

- init
{
  [super init];
  return self;
}

- free
{
  return [super free];
}

- (int)duration
{
  return [durationField intValue];
}

- (const char *)startDateString
{
  return [startDateField stringValue];
}

- (const char *)startTimeString
{
  return [startTimeField stringValue];
}

- (const char *)description
{
  return [descriptionField stringValue];
}

- (void)clearFields
{
  [durationField    setStringValue:""];
  [startTimeField   setStringValue:""];
  [startDateField   setStringValue:""];
  [descriptionField setStringValue:""];
}

/*
 * Display the contents of a session object
 */
- (void)loadSession:(Session *)session
{
  if ( session == nil ) {
    [self clearFields];
    [startTimeField setStringValue:currentTime()];
    [startDateField setStringValue:currentDate()];
  } else {
    [durationField setIntValue:[session minutes]];
    [startTimeField setStringValue:[session startTimeString]];
    [startDateField setStringValue:[session startDateString]];
    [descriptionField setStringValue:[session description]];
  }
}

/*
 * Copy the data from the form into the session object. If the
 * object is nil, allocate one and return it.
 */
- (Session *)saveSession:(Session *)session
{
  const char *duration;
  char *ptr;
  int minutes;

  if ( session == nil )
    session = [[Session alloc] init];
  
  duration = [durationField stringValue];
  if ( (ptr = strchr( duration, ':' )) != NULL )
    minutes = atoi( duration ) * 60 + atoi( ++ptr );
  else if ( (ptr = strchr( duration, '.' )) != NULL )
    minutes = atof( duration ) * 60;
  else
    minutes = atoi( duration );

  [session setMinutes:minutes];
  [session setStartTimeString:[startTimeField stringValue]];
  [session setStartDateString:[startDateField stringValue]];
  [session setDescription:    [descriptionField stringValue]];

  return session;
}

/*
 * This routine should check that there is enough data to be useful...
 */
- (Session *)editItem:(Session *)session
{
  int value ;

  [self loadSession:session];
  [form selectTextAt:0];	/* always select the first field */

  value = [NXApp runModalFor:panel] ;
  [panel close];

  switch ( value  ) {
  case NX_RUNSTOPPED:
    return [self saveSession:session];	/* copy data back into struct */

  default:
  case NX_RUNABORTED:
    return nil;
  }
}

- cancel:sender
{
  [NXApp abortModal];
  return self;
}

- ok:sender
{
  int dummy;

  /* This is pretty cheesey. */
  if ( sscanf([startDateField stringValue],
	      "%d/%d/%d", &dummy, &dummy, &dummy ) != 3 ||
       sscanf([startTimeField stringValue],
	      "%d:%d", &dummy, &dummy ) != 2 ) {
    NXRunAlertPanel( [NXApp appName], "Valid date (mm/dd/yy) and time (hh:mm) required.",
		    "It won't happen again", NULL, NULL );
  } else
    [NXApp stopModal];

  return self;
}

@end


